const Ws = require('ws')
const server = new Ws.Server({port:8000})
const init = () => {
    bindEvent()
    server.clients.forEach(c=>{
        // console.log(c)
        c.send('hahahah')
    })
}
function bindEvent () {
    server.on('open',handlerOpen);
    server.on('close',handlerClose);
    server.on('error',handlerError);
    server.on('connection',handlerMessage);
}
function handlerOpen(){
    console.log("open")
}
function handlerClose(){
    console.log("close")
}
function handlerError(){
    console.log("error")
}
function handlerMessage(ws){
    console.log("message")
    ws.on("message",handlerMsg)
}
function handlerMsg(msg){
    // console.log(server.clients)
    server.clients.forEach(c=>{
        console.log(c)
        c.send(JSON.stringify({a:c}))
    })
}
init()
